package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.XxCjBean;
import com.mm.dss.demo.bean.YhdBean;
import com.mm.dss.demo.bean.YjxxBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
* 预警信息
* */
public class YjxxActivity extends BaseActivity {
    TextView txt_title;
    RelativeLayout layout_back;
    RecyclerView recyclerView;
    List<String> timelist=new ArrayList<>();
    List<String> contentlist=new ArrayList<String>();
    List<String> list=new ArrayList<>();//预警点
    BaseRecyclerAdapter<YjxxBean> baseRecyclerAdapter;
    List<YjxxBean> yjxxBeanlist=new ArrayList<>();
    private Dialog mWeiboDialog;    private SwipeRefreshLayout swipregresh;


    private void setnet(boolean IsDialog) {
        if (yjxxBeanlist.size()>0){
            yjxxBeanlist.clear(); }
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(YjxxActivity.this,
                            "加载中...");
        }
        Map<String,String> map=new HashMap<>();
        map.put("userid",SharedPreferencedUtils.getString(YjxxActivity.this,"username"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N11_GetYJXXList", null, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                Gson gson=new Gson();
                try {
                    JSONArray array=new JSONArray(result.getProperty(0).toString());
                    for (int i=0;i<array.length();i++){
                        YjxxBean yjxxBean=gson.fromJson(array.optJSONObject(i).toString(),YjxxBean.class);
                        yjxxBeanlist.add(yjxxBean);
                    }
                    baseRecyclerAdapter.setData(yjxxBeanlist);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yjxx);
        initview();
        initdata();
    }

    private void initview() {
        txt_title= (TextView) findViewById(R.id.txt_title);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        txt_title.setText("预警信息");
        layout_back.setVisibility(View.VISIBLE);
        swipregresh= (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initSwipeRefresh();
    }


    protected void initdata() {
        baseRecyclerAdapter=new BaseRecyclerAdapter<YjxxBean>(this,null,R.layout.item_yjxxview) {
            @Override
            protected void convert(BaseViewHolder helper, final YjxxBean item) {
       helper.setText(R.id.didian,item.getBIAOTI());
       helper.setText(R.id.txt,item.getNEIRONG());
       helper.setText(R.id.time,item.getSHIJIAN());
       helper.getConvertView().setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Intent intent=new Intent(YjxxActivity.this,YjxxinfoActivity.class);
               intent.putExtra("title",item.getBIAOTI());
               intent.putExtra("txt",item.getNEIRONG());
               intent.putExtra("time",item.getSHIJIAN());
               startActivity(intent);
           }
       });
            }
        };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);
     setnet(true);


    }


    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
